import asyncio
from playwright.async_api import async_playwright

async def screenshot_chart(url, output_path='chart.png'):
    async with async_playwright() as p:
        # 启动浏览器，使用 Chromium 浏览器
        browser = await p.chromium.launch(headless=True)
        page = await browser.new_page()

        # 打开指定的 URL
        await page.goto(url)

        # 等待图表（假设是 canvas 元素）加载完成
        #await page.wait_for_selector('canvas[aria-label="Chart for DATA, 1 hour"]',  state='visible')
        await page.wait_for_load_state('networkidle')
        await asyncio.sleep(3)

        # 截图并保存
        await page.screenshot(path=output_path)

        # 关闭浏览器
        await browser.close()

# 指定图表页面的 URL
url = 'https://www.gmgn.cc/kline/sol/8YyjnnGYNXvKLZs5FaiC3h7KbwpHSeKj1VkaFFXqpump'  # 替换为图表的 URL

# 调用函数并保存截图
asyncio.run(screenshot_chart(url, 'chart.png'))